﻿
games.game4 = {
    //bomberman
    worldmap: [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
               [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0],
               [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
               [0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
               [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
    renderloop: undefined,
    npcRenderLoop: undefined,
    npcThinkLoop: undefined,
    stopGame: function () {
        window.clearInterval(this.renderloop);
        window.clearInterval(this.npcRenderLoop);
        window.clearInterval(this.npcThinkLoop);
    },
    init: function () {
        var html = "";
        html += "<table id='game4-bomberman'>";
        html += "<tr><td colspan='30'>you are the red box, up, down, left, right to move. x to lay a mine, and x again to blow the mine.</td></tr>";
        for (var i = 0; i < this.worldmap.length; i++) {
            var worldmapRow = this.worldmap[i];
            html += "<tr class='row{0}'>".replace("{0}", i);
            for (var k = 0; k < worldmapRow.length; k++) {
                var worldmapCol = worldmapRow[k];
                html += "<td class='col{1} {0}'>&nbsp;</td>".replace("{1}", k).replace("{0}", (worldmapCol == 0 ? "wall" : worldmapCol == 1 ? "floor" : "obstacle"));
            }
            html += "</tr>";
        }
        html += "</table>";
        $("#gamezone").html(html);
        $(document).keyup(function (event) {
            games.game4.player.handleKey(event.keyCode, true);
            games.game4.player.handleKey(event.keyCode, false);
            return false;
        });
        this.renderloop = setInterval(function () {
            games.game4.player.draw($("#game4-bomberman"));
            $("#game4-bomberman .npc").removeClass("npc");
            for (var i = 0; i < games.game4.npcs.length; i++) {
                if (i < games.game4.npcs.length) {
                    var coords = games.game4.npcs[i];
                    games.game4.npc.draw(coords);
                }
            }
        }, 10);
        this.npcThinkLoop = setInterval(function () {
            for (var i = 0; i < games.game4.npcs.length; i++) {
                if (i < games.game4.npcs.length) {
                    var coords = games.game4.npcs[i];
                    if (coords[0] > 0 && coords[1] > 0) {
                        coords = games.game4.npc.think(coords)
                    }
                }
            }
        }, 250);
        this.npcRenderLoop = setInterval(function () {
            games.game4.npcs[games.game4.npcs.length] = [games.helper.getRandomNumber(10, 20), games.helper.getRandomNumber(10, 20)];
            games.game4.npc.timeOfBirth -= 100;
            if (games.game4.npc.timeOfBirth < 1000) {
                games.game4.npc.timeOfBirth = 1000
            }
        }, games.game4.npc.timeOfBirth);
    },
    bombHelper: {
        explode: function (x, y) {
            $("#game4-bomberman .row{0} .col{1}".replace("{0}", x).replace("{1}", y)).removeClass("bomb");
            for (var ix = -1; ix <= 1; ix++) {
                for (var iy = -1; iy <= 1; iy++) {
                    $("#game4-bomberman .row{0} .col{1}".replace("{0}", ix + x).replace("{1}", iy + y)).addClass("explosion");
                    for (var i = 0; i < games.game4.npcs.length; i++) {
                        var coords = games.game4.npcs[i];
                        if ((coords[1] == ix + x && coords[0] == iy + y)) {
                            games.game4.npcs[i] = [-1, -1];
                            games.game4.player.score++;
                        }
                    }
                }
            }
            setTimeout(function () {
                games.game4.bombHelper.implode(x, y);
            }, 125);
        },
        implode: function (x, y) {
            for (var ix = -1; ix <= 1; ix++) {
                for (var iy = -1; iy <= 1; iy++) {
                    if (ix != 0 && iy != 0)
                        $("#game4-bomberman .row{0} .col{1}".replace("{0}", ix + x).replace("{1}", iy + y)).removeClass("explosion");
                    for (var i = 0; i < games.game4.npcs.length; i++) {
                        var coords = games.game4.npcs[i];
                        if ((coords[1] == ix + x && coords[0] == iy + y)) {
                            games.game4.npcs[i] = [-1, -1];
                            games.game4.player.score++;
                        }
                    }
                }
            }
            setTimeout(function () {
                games.game4.bombHelper.vanish(x, y);
            }, 250);
        },
        vanish: function (x, y) {
            for (var ix = -1; ix <= 1; ix++) {
                for (var iy = -1; iy <= 1; iy++) {
                    $("#game4-bomberman .row{0} .col{1}".replace("{0}", ix + x).replace("{1}", iy + y)).removeClass("explosion");
                    for (var i = 0; i < games.game4.npcs.length; i++) {
                        var coords = games.game4.npcs[i];
                        if ((coords[1] == ix + x && coords[0] == iy + y)) {
                            games.game4.npcs[i] = [-1, -1];
                            games.game4.player.score++;
                        }
                    }
                }
            }
        }
    },
    npcs: new Array(),
    npc: {
        timeOfBirth: 5000,
        think: function (coords) {
            //decide new co-ords
            var choice = games.helper.getRandomNumber(0, 100);
            var x = coords[0];
            var y = coords[1];
            if (choice < 25) {
                x--;
            } else if (choice < 50) {
                y--;
            } else if (choice < 75) {
                x++;
            } else if (choice < 100) {
                y++;
            }
            var wallHit = $("#game4-bomberman .row{0} .col{1}".replace("{0}", y).replace("{1}", x)).hasClass("wall");
            if (!wallHit) {
                coords[0] = x;
                coords[1] = y;
            }
            return coords;
        },
        draw: function (coords) {
            $("#game4-bomberman .row{0} .col{1}".replace("{0}", coords[1]).replace("{1}", coords[0])).addClass("npc");
        }
    },
    player: {
        score: 0,
        coords: [1, 1],
        bombPosition: undefined,
        handleKey: function (charCode, isUp) {
            //81 drop bomb blow up bomb
            if (isUp) {
                //fire only
                switch (charCode) {
                    case 88:
                        if (games.game4.player.bombPosition == undefined) {
                            games.game4.player.bombPosition = new Array();
                            games.game4.player.bombPosition[0] = games.game4.player.coords[0];
                            games.game4.player.bombPosition[1] = games.game4.player.coords[1];
                        } else {
                            games.game4.bombHelper.explode(games.game4.player.bombPosition[1], games.game4.player.bombPosition[0]);
                            games.game4.player.bombPosition = undefined;
                        }
                        break;
                }
            } else {
                var x = games.game4.player.coords[0];
                var y = games.game4.player.coords[1];
                switch (charCode) {
                    case 37:
                        x--;
                        break;
                    case 38:
                        y--;
                        break;
                    case 39:
                        x++;
                        break;
                    case 40:
                        y++;
                        break;
                }
                var wallHit = $("#game4-bomberman .row{0} .col{1}".replace("{0}", y).replace("{1}", x)).hasClass("wall");
                if (!wallHit) {
                    games.game4.player.coords[0] = x;
                    games.game4.player.coords[1] = y;
                }
            }
            if (charCode == 37 || charCode == 38 || charCode == 39 || charCode == 40 || charCode == 88) {
                return false;
            }
        },
        draw: function (table) {
            $("#game4-bomberman td").removeClass("player")
            $("#game4-bomberman .row{0} .col{1}".replace("{0}", this.coords[1]).replace("{1}", this.coords[0])).addClass("player");
            for (var i = 0; i < games.game4.npcs.length; i++) {
                var npcCoords = games.game4.npcs[i];
                if (npcCoords[0] == this.coords[0] && npcCoords[1] == this.coords[1]) {
                    $("#game4-bomberman td").removeClass("player")
                    alert("uh oh, you didn't make it. Score was " + this.score);
                    games.game4.stopGame();
                }
            }
            if (this.bombPosition != undefined) {
                $("#game4-bomberman .row{0} .col{1}".replace("{0}", this.bombPosition[1]).replace("{1}", this.bombPosition[0])).addClass("bomb");
            }
            //games.game4.player.bombPosition
        }
    }
};